﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;

namespace TransCend.Util
{
    class security
    {
        //public string md5(string content) {
        //    return FormsAuthentication.HashPasswordForStoringInConfigFile(content,"md5");
        //}
        /// <summary>
        ///     返回MD5
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetMd5Hash(string input,Encoding encoding)
        {
            //MD5 md5Hash = MD5.Create();
            //byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            //StringBuilder sBuilder = new StringBuilder();

            //for (int i = 0; i < data.Length; i++)
            //{
            //    sBuilder.Append(data[i].ToString("x2"));
            //}

            //return sBuilder.ToString();

            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            byte[] data = provider.ComputeHash(encoding.GetBytes(input));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(data[i].ToString("x2"));
            }

            return builder.ToString();
        }
        public static string GetUTF8MD5Hash(string input) {
            return GetMd5Hash(input, Encoding.UTF8);
        }
        /// <summary>
        /// 以GB2312方式对字符串进行MD5加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetGB2312MD5Hash(string input)
        {
            return GetMd5Hash(input, Encoding.GetEncoding("GB2312"));
        }
        public static string GetSHA1(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            //str_sha1_out = str_sha1_out.Replace("-", "");
            return str_sha1_out;
        }
    }
}
